package com.yhm.myfootbollshow.demos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhm.myfootbollshow.demos.base.ResVo;
import com.yhm.myfootbollshow.demos.entity.po.Cart;
import com.yhm.myfootbollshow.demos.entity.vo.CartVo;
import com.yhm.myfootbollshow.demos.service.CartService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: yhm
 * @time: 2025/4/15 17:45
 */
@RestController
@RequestMapping("yhm/cart")
public class CartController {
    @Resource
    private CartService cartService;

    @GetMapping("list")
    public ResVo<List<CartVo>> list(Long userId) {
        return ResVo.ok(cartService.list(userId));
    }

    @PostMapping("add")
    public ResVo<Boolean> add(Cart cart) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, cart.getUserId());
        wrapper.eq(Cart::getProductId, cart.getProductId());
        Cart one = cartService.getOne(wrapper);
        if (one != null){
            one.setQuantity(one.getQuantity()+1);
            return ResVo.ok(cartService.updateById(one));
        }
        return ResVo.ok(cartService.save(cart));
    }

    @PostMapping("update")
    public ResVo<Boolean> update(Cart cart) {
        // 判断是否为空
        if (cart.getCartId() == null || cart.getUserId() == null || cart.getProductId() == null || cart.getQuantity() == null) {
            return ResVo.error(false);
        }
        return ResVo.ok(cartService.updateById(cart));
    }
    @PostMapping("del")
    public ResVo<Boolean> del(Long cartId) {
        return ResVo.ok(cartService.removeById(cartId));
    }
}
